package cn.mxz.robots.basic.local;

import java.net.URL;
import java.util.ArrayList;
import java.util.TreeMap;

import cn.mxz.crawler_single.basic.ICrawler;
import cn.mxz.crawler_single.basic.Request;
import cn.mxz.crawler_single.cache.Ocs;
import cn.mxz.crawler_single.util.Encrypt;
import cn.mxz.crawler_single.CrawlerFactory;
import cn.mxz.rds.pojo.GaoKaoRank;
import cn.mxz.robots.pojo.Student;
import cn.mxz.robots.util.RankUtil;
import cn.mxz.robots.util.RobotsUtlis;

/**
 * 高考超级类
 * 
 * @author cuizhennan<cuizhennan@mouge.wang>
 * @since 2015-06-18
 */
public abstract class Exam implements IExam, IStatus
{
	protected static final String WEN_KE = "bw";
	protected static final String LI_KE = "bl";

	protected static final String BEN_KE_WEN_KE = "bw";
	protected static final String BEN_KE_LI_KE = "bl";
	protected static final String ZHUAN_KE_WEN_KE = "zw";
	protected static final String ZHUAN_KE_LI_KE = "zl";

	protected Student mStudent;
	protected String mSha1;
	protected ICrawler mCrawler;

	public Exam(Student student)
	{
		mStudent = student;
		mSha1 = Encrypt.sha1(student.getIdCard() + "_gk");
		mCrawler = CrawlerFactory.createDefalut();
	}

	/**
	 * 查詢登录
	 * 
	 * @param captcha
	 *            验证码
	 * @return 登录结果
	 */
	public abstract String login(String captcha);

	/**
	 * 获取登录时候的验证码
	 * 
	 * @return 验证码地址
	 */
	public String getCaptcha()
	{
		return NO_CAPTCHA;
	}

	/**
	 * 封装了底层爬虫的curlPost请求，能够过滤掉无效的返回结果
	 * 
	 * @param request
	 *            http请求信息
	 * @return 请求结果
	 */
	protected String curlPost(Request request)
	{
		return mCrawler.curlPost(request);
	}

	/**
	 * 封装了底层爬虫的curlFile请求
	 * 
	 * @param request
	 *            http请求信息
	 * @return 请求结果
	 */
	protected String curlFile(Request request)
	{
		return mCrawler.curlFile(request);
	}

	/**
	 * 封装了底层爬虫的curlGet请求
	 * 
	 * @param request
	 *            http请求信息
	 * @return 请求结果
	 */
	protected String curlGet(Request request)
	{
		return mCrawler.curlGet(request);
	}

	/**
	 * 正则匹配
	 * 
	 * @param str
	 *            要匹配的字符串
	 * @param regex
	 *            匹配的规则
	 * @return 匹配成功的字符串
	 */
	protected static String regexMatch(String str, String regex)
	{
		return RobotsUtlis.regexMatch(str, regex);
	}

	/**
	 * 根据参数的类型和参数，构造出一个Url
	 * 
	 * @param type
	 *            需要的Url的类型
	 * @param params
	 *            需要构造的Url的参数
	 * @return 构造好的Url
	 */
	// protected abstract String buildUrl(String type, String params);

	/**
	 * 将字符串进行URLEncode加密
	 * 
	 * @param input
	 *            加密之前的字符串,编码默认为GB2312
	 * @return 加密之后的字符串
	 */
	protected String urlChineseEncode(String input)
	{
		return RobotsUtlis.urlChineseEncode(input);
	}

	/*
	 * 获取地址栏的加密token
	 */
	public String regexToken()
	{
		/* 获取token */
		String tokenUrl = getCache(RobotsUtlis.getTokenUrlKey(mSha1));

		/* 匹配tokenUrl */
		return regexMatch(tokenUrl, "\\/\\((.*)\\)\\/");
	}

	/**
	 * 格式化URL
	 */
	protected static URL getURL(String url)
	{
		return RobotsUtlis.getURL(url);
	}

	protected void setCache(String key, String value)
	{
		Ocs.set(key, value);
	}

	protected String getCache(String key)
	{
		return Ocs.get(key);
	}

	protected void delCookie()
	{
		Ocs.del(mSha1);
	}

	protected void setRank(TreeMap<Integer, Integer> scoreRange, String type, Integer provinceId)
	{
		RankUtil.set(scoreRange, type, provinceId);
	}

	protected void setRank(ArrayList<GaoKaoRank> ranks, Integer provinceId)
	{
		RankUtil.set(ranks, provinceId);
	}

	protected Integer getRank(Integer provinceId, String score, String type)
	{
		return RankUtil.get(provinceId, score, type);
	}

	/**
	 * 根据去年省份的ID，今年的排名和文理科类型，获取去年的分数
	 * 
	 * @param provinceId
	 *            去年省份ID
	 * @param ranking
	 *            今年排名
	 * @param type
	 *            文理科类型
	 * @return 去年的分数
	 */
	protected Integer getTransformScore(Integer provinceId, Integer ranking, String type)
	{
		return RankUtil.getTransformScore(provinceId, ranking, type);
	}
}
